import { ProductServiceClient } from "proto-ts/center-product/product";
import { AccountServiceClient } from "proto-ts/center-product/account";
import * as dependency_1 from "proto-ts/center-product/base";

import { credentials } from "@grpc/grpc-js";
import { Service } from "typedi";
import { GRPC_ADDRESS_2 } from "config/config";
import { setReqHeader, REPO_ERR, RES_INFO } from "config/const";
import { HttpParams } from "client/configVo";

const productConn = new ProductServiceClient(
  GRPC_ADDRESS_2,
  credentials.createInsecure()
);

const accountConn = new AccountServiceClient(
  GRPC_ADDRESS_2,
  credentials.createInsecure()
);

export const REPO_PRODUCT_INTERFACE = "repoProductInterface";

export interface productEntity {
  ID: number; // 唯一ID
  CreatedAt?: string; // 创建时间
  UpdatedAt?: string; // 更新时间
  DeletedAt?: string; // 删除时间
  OrganizationID?: number; // 关联组织ID
  AccountID?: number; // 关联账户ID
}

export interface productEntity {
  Type?: string;
  UID?: string;
}

export interface repoProductInterface {
  getGoodsByIds(opts: HttpParams): Promise<productEntity | any>;
  getSitesDetail(opts: HttpParams): Promise<productEntity | any>;
  getHelp(opts: HttpParams): Promise<productEntity | any>;
  get_detail(opts: HttpParams): Promise<productEntity | any>;
}

@Service(REPO_PRODUCT_INTERFACE)
class repo implements repoProductInterface {
  // 获取账号列表
  async getSitesDetail(opts: HttpParams): Promise<any> {
    const { request, response, userInfo, query, fields } = opts;
    const header = setReqHeader(userInfo);
    return new Promise<any>((resolve, reject) => {
      accountConn.List(
        new dependency_1.BasePlatformListRequest({
          platform: query.platform,
          param: new dependency_1.ListParam({
            fields: fields || [
              "id",
              "name",
              "platform",
              "states",
              "attributes",
            ],
            filters: [new dependency_1.Filter(query)],
          }),
        }),
        header,
        (err, resp) => {
          if (err) return resolve(REPO_ERR(err, "获取账号列表详情"));
          const setting = <any>resp?.toObject();
          resolve(
            RES_INFO({
              data: setting?.list || [],
              total: setting?.total || 0,
            })
          );
        }
      );
    });
  }

  // 获取某个平台的文档地址
  async getHelp(opts: HttpParams): Promise<any> {
    const { request, response, userInfo } = opts;
    const header = setReqHeader(userInfo);
    const query = request.body;
    return new Promise<any>((resolve, reject) => {
      accountConn.GetHelper(
        new dependency_1.BasePlatformRequest(query),
        header,
        (err, resp) => {
          if (err) return resolve(REPO_ERR(err, "获取帮助文档地址"));
          const setting = <any>resp?.toObject();
          resolve(RES_INFO({ data: setting?.url }));
        }
      );
    });
  }

  // 根据商品ID获数取商品列表
  getGoodsByIds(opts: HttpParams): Promise<any> {
    const { request, response, userInfo, query } = opts;
    const header = setReqHeader(userInfo);
    return new Promise<any>((resolve, reject) => {
      productConn.List(
        new dependency_1.BaseListRequest({
          param: new dependency_1.ListParam({
            fields: [
              "account_id",
              "id",
              "collection_ids",
              "image",
              "meta",
              "title",
              "sku",
              "sub_products",
            ],
            filters: query.map((item: any) => {
              return new dependency_1.Filter(item);
            }),
          }),
        }),
        header,
        (err, resp) => {
          if (err) return resolve(REPO_ERR(err, "根据商品ID获数取商品列表"));
          const setting = <any>resp?.toObject();
          resolve(
            RES_INFO({
              data: {
                list: setting?.list,
              },
            })
          );
        }
      );
    });
  }

  // 查询商品详情
  async get_detail(opts: HttpParams): Promise<any> {
    const { request, response, userInfo } = opts;
    const header = setReqHeader(userInfo);
    const query = request.body;
    return new Promise<any>((resolve, reject) => {
      productConn.Detail(
        new dependency_1.BaseIdRequest(query),
        header,
        (err, resp) => {
          if (err) return resolve(REPO_ERR(err, "查询商品详情"));
          const setting = <any>resp?.toObject();
          resolve(RES_INFO({ data: setting?.data || {} }));
        }
      );
    });
  }
}
